package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import server.GetRadomValue;
import service.tDeviceService;
import util.UtilCommon;
import entity.tDevice;

public class RunDeviceAction
{
    private String deviceId;
    
    private int ph;
    
    private int orp;
    
    private int temperature;
    
    private int ec;
    
    private int openEC;
    
    private int closeEC;
    
    private int esil;
    
    private int ec1;
    
    private int ec2;
    
    private int ec3;
    
    private int ec4;
    
    private int ec5;
    
    private int ec6;
    
    private int ec7;
    
    private int ec8;
    
    private int ec9;
    
    private int ec10;
    
    private int ec11;
    
    private int ec12;
    
    private int ec13;
    
    private String mac;
    
    private String deviceName;
    
    private String place;
    
    public static final int SERVERPORT = 55555;
    
    private tDeviceService tdeviceService;
    
    private boolean whether = false;
    
    private LinkedList<String> valueList = new LinkedList<String>();
    
    private LinkedList<String> valueList1 = new LinkedList<String>();
    
    private LinkedList<String> valueList2 = new LinkedList<String>();
    
    private LinkedList<String> valueList3 = new LinkedList<String>();
    
    private String[] dateArray = null;
    
    private int index;
    
    public String run()
        throws IOException, ParseException
    {
        GetRadomValue.getInstance().setIfUse(true);
        ServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        List<tDevice> list = tdeviceService.searchById(deviceId);
        this.setDeviceName(list.get(0).getDeviceName());
        this.setPlace(list.get(0).getPlace());
        this.setMac(list.get(0).getMac());
        int ph = list.get(0).getPh();
        int orp = list.get(0).getOrp();
        int temperature = list.get(0).getTemperature();
        int ec = list.get(0).getEc();
        int openEC = list.get(0).getOpenEC();
        int closeEC = list.get(0).getCloseEC();
        this.setPh(ph);
        this.setOrp(orp);
        this.setTemperature(temperature);
        this.setEc(ec);
        this.setOpenEC(openEC);
        this.setCloseEC(closeEC);
        
        this.esil = list.get(0).getEsil();
        this.ec1 = list.get(0).getEc1();
        this.ec2 = list.get(0).getEc2();
        this.ec3 = list.get(0).getEc3();
        this.ec4 = list.get(0).getEc4();
        this.ec5 = list.get(0).getEc5();
        this.ec6 = list.get(0).getEc6();
        this.ec7 = list.get(0).getEc7();
        this.ec8 = list.get(0).getEc8();
        this.ec9 = list.get(0).getEc9();
        this.ec10 = list.get(0).getEc10();
        this.ec11 = list.get(0).getEc11();
        this.ec12 = list.get(0).getEc12();
        this.ec13 = list.get(0).getEc13();
        return "success";
    }
    
    public String oftenRun()
        throws IOException, ParseException
    {
        GetRadomValue.getInstance().setIfUse(true);
        ServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String[] dateArray = null;
        dateArray = GetRadomValue.getInstance().getArrayListValue(this.getMac());
        JSONObject result = new JSONObject();
        if (dateArray != null)
        {
            result.put("ph", String.valueOf(Float.parseFloat(dateArray[0]) / 100));
            result.put("orp", String.valueOf(Float.parseFloat(dateArray[1])));
            result.put("temperature", String.valueOf(Float.parseFloat(dateArray[2]) / 10));
            result.put("ec", String.valueOf(UtilCommon.formatFloat(Float.parseFloat(dateArray[3]) / 10)));
            result.put("openEC", String.valueOf(Float.parseFloat(dateArray[4])));
            result.put("closeEC", String.valueOf(Float.parseFloat(dateArray[5])));
            result.put("esil", String.valueOf(Float.parseFloat(dateArray[7])));
            result.put("ec1", String.valueOf(Float.parseFloat(dateArray[8]) / 10));
            result.put("ec2", String.valueOf(Float.parseFloat(dateArray[9]) / 10));
            result.put("ec3", String.valueOf(Float.parseFloat(dateArray[10]) / 10));
            result.put("ec4", String.valueOf(Float.parseFloat(dateArray[11]) / 10));
            result.put("ec5", String.valueOf(Float.parseFloat(dateArray[12]) / 10));
            result.put("ec6", String.valueOf(Float.parseFloat(dateArray[13]) / 10));
            result.put("ec7", String.valueOf(Float.parseFloat(dateArray[14]) / 10));
            result.put("ec8", String.valueOf(Float.parseFloat(dateArray[15]) / 10));
            result.put("ec9", String.valueOf(Float.parseFloat(dateArray[16]) / 10));
            result.put("ec10", String.valueOf(Float.parseFloat(dateArray[17]) / 10));
            result.put("ec11", String.valueOf(Float.parseFloat(dateArray[18]) / 10));
            result.put("ec12", String.valueOf(Float.parseFloat(dateArray[19]) / 10));
            result.put("ec13", String.valueOf(Float.parseFloat(dateArray[20]) / 10));
        }
        out.write(result.toString());
        out.flush();
        out.close();
        return null;
    }
    
    @SuppressWarnings("unchecked")
    public String oftenChartRun()
        throws IOException, ParseException
    {
        JSONArray data = null;
        ServletResponse response = ServletActionContext.getResponse();
        dateArray = GetRadomValue.getInstance().getArrayListValue(this.getMac());
        if (dateArray != null)
        {
            if (this.getIndex() == 0)
            {
                GetRadomValue.getInstance().pushValue(dateArray[this.getIndex()]);
                valueList = GetRadomValue.getInstance().getRadomValue();
                data = JSONArray.fromObject(valueList);
            }
            else if (this.getIndex() == 1)
            {
                GetRadomValue.getInstance().pushValue1(dateArray[this.getIndex()]);
                valueList1 = GetRadomValue.getInstance().getRadomValue1();
                data = JSONArray.fromObject(valueList1);
            }
            else if (this.getIndex() == 2)
            {
                GetRadomValue.getInstance().pushValue2(dateArray[this.getIndex()]);
                valueList2 = GetRadomValue.getInstance().getRadomValue2();
                data = JSONArray.fromObject(valueList2);
            }
            else
            {
                GetRadomValue.getInstance().pushValue3(dateArray[this.getIndex()]);
                valueList3 = GetRadomValue.getInstance().getRadomValue3();
                data = JSONArray.fromObject(valueList3);
            }
        }
        response.setContentType("text/html;charset=UTF-8");
        if (data != null)
        {
            response.getWriter().print(data.toString());
        }
        else
        {
            response.getWriter().print("[[]]");
        }
        
        return null;
    }
    
    public String getX()
    {
        ServletResponse response = ServletActionContext.getResponse();
        List<String[]> list = new ArrayList<String[]>();
        Calendar Cal = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        int i;
        if (valueList.size() < 5)
        {
            for (i = 0; i < 21; i++)
            {
                Date clock = new Date();
                Cal.setTime(clock);
                if (i == 0)
                {
                    Cal.add(java.util.Calendar.SECOND, -0);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else
                {
                    String[] s = {i + "", "  "};
                    list.add(i, s);
                }
            }
        }
        else if (valueList.size() < 10)
        {
            for (i = 0; i < 21; i++)
            {
                Date clock = new Date();
                Cal.setTime(clock);
                if (i == 0)
                {
                    Cal.add(java.util.Calendar.SECOND, -25);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 5)
                {
                    Cal.add(java.util.Calendar.SECOND, -0);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else
                {
                    String[] s = {i + "", "  "};
                    list.add(i, s);
                }
            }
        }
        else if (valueList.size() < 15)
        {
            for (i = 0; i < 21; i++)
            {
                Date clock = new Date();
                Cal.setTime(clock);
                if (i == 0)
                {
                    Cal.add(java.util.Calendar.SECOND, -50);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 5)
                {
                    Cal.add(java.util.Calendar.SECOND, -25);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 10)
                {
                    Cal.add(java.util.Calendar.SECOND, -25);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else
                {
                    String[] s = {i + "", "  "};
                    list.add(i, s);
                }
            }
        }
        else if (valueList.size() < 20)
        {
            for (i = 0; i < 21; i++)
            {
                Date clock = new Date();
                Cal.setTime(clock);
                if (i == 0)
                {
                    Cal.add(java.util.Calendar.SECOND, -75);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 5)
                {
                    Cal.add(java.util.Calendar.SECOND, -50);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 10)
                {
                    Cal.add(java.util.Calendar.SECOND, -25);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 15)
                {
                    Cal.add(java.util.Calendar.SECOND, -0);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else
                {
                    String[] s = {i + "", "  "};
                    list.add(i, s);
                }
            }
        }
        else
        {
            for (i = 0; i < 21; i++)
            {
                Date clock = new Date();
                Cal.setTime(clock);
                if (i == 0)
                {
                    Cal.add(java.util.Calendar.SECOND, -100);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 5)
                {
                    Cal.add(java.util.Calendar.SECOND, -75);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 10)
                {
                    Cal.add(java.util.Calendar.SECOND, -50);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 15)
                {
                    Cal.add(java.util.Calendar.SECOND, -25);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else if (i == 20)
                {
                    Cal.add(java.util.Calendar.SECOND, -0);
                    String time = formatter.format(Cal.getTime());
                    String[] s = {i + "", time};
                    list.add(i, s);
                }
                else
                {
                    String[] s = {i + "", "  "};
                    list.add(i, s);
                }
                
            }
        }
        JSONArray data = JSONArray.fromObject(list);
        response.setContentType("text/html;charset=UTF-8");
        try
        {
            response.getWriter().print(data.toString());
            
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }
    
    public tDeviceService getTdeviceService()
    {
        return tdeviceService;
    }
    
    public void setTdeviceService(tDeviceService tdeviceService)
    {
        this.tdeviceService = tdeviceService;
    }
    
    public int getPh()
    {
        return ph;
    }
    
    public void setPh(int ph)
    {
        this.ph = ph;
    }
    
    public int getOrp()
    {
        return orp;
    }
    
    public void setOrp(int orp)
    {
        this.orp = orp;
    }
    
    public int getTemperature()
    {
        return temperature;
    }
    
    public void setTemperature(int temperature)
    {
        this.temperature = temperature;
    }
    
    public int getEc()
    {
        return ec;
    }
    
    public void setEc(int ec)
    {
        this.ec = ec;
    }
    
    public int getOpenEC()
    {
        return openEC;
    }
    
    public void setOpenEC(int openEC)
    {
        this.openEC = openEC;
    }
    
    public int getCloseEC()
    {
        return closeEC;
    }
    
    public void setCloseEC(int closeEC)
    {
        this.closeEC = closeEC;
    }
    
    public boolean isWhether()
    {
        return whether;
    }
    
    public void setWhether(boolean whether)
    {
        this.whether = whether;
    }
    
    public String getDeviceId()
    {
        return deviceId;
    }
    
    public void setDeviceId(String deviceId)
    {
        this.deviceId = deviceId;
    }
    
    public String getMac()
    {
        return mac;
    }
    
    public void setMac(String mac)
    {
        this.mac = mac;
    }
    
    public int getIndex()
    {
        return index;
    }
    
    public void setIndex(int index)
    {
        this.index = index;
    }
    
    public String getDeviceName()
    {
        return deviceName;
    }
    
    public void setDeviceName(String deviceName)
    {
        this.deviceName = deviceName;
    }
    
    public String getPlace()
    {
        return place;
    }
    
    public void setPlace(String place)
    {
        this.place = place;
    }
    
    public int getEsil()
    {
        return esil;
    }
    
    public void setEsil(int esil)
    {
        this.esil = esil;
    }
    
    public int getEc1()
    {
        return ec1;
    }
    
    public void setEc1(int ec1)
    {
        this.ec1 = ec1;
    }
    
    public int getEc2()
    {
        return ec2;
    }
    
    public void setEc2(int ec2)
    {
        this.ec2 = ec2;
    }
    
    public int getEc3()
    {
        return ec3;
    }
    
    public void setEc3(int ec3)
    {
        this.ec3 = ec3;
    }
    
    public int getEc4()
    {
        return ec4;
    }
    
    public void setEc4(int ec4)
    {
        this.ec4 = ec4;
    }
    
    public int getEc5()
    {
        return ec5;
    }
    
    public void setEc5(int ec5)
    {
        this.ec5 = ec5;
    }
    
    public int getEc6()
    {
        return ec6;
    }
    
    public void setEc6(int ec6)
    {
        this.ec6 = ec6;
    }
    
    public int getEc7()
    {
        return ec7;
    }
    
    public void setEc7(int ec7)
    {
        this.ec7 = ec7;
    }
    
    public int getEc8()
    {
        return ec8;
    }
    
    public void setEc8(int ec8)
    {
        this.ec8 = ec8;
    }
    
    public int getEc9()
    {
        return ec9;
    }
    
    public void setEc9(int ec9)
    {
        this.ec9 = ec9;
    }
    
    public int getEc10()
    {
        return ec10;
    }
    
    public void setEc10(int ec10)
    {
        this.ec10 = ec10;
    }
    
    public int getEc11()
    {
        return ec11;
    }
    
    public void setEc11(int ec11)
    {
        this.ec11 = ec11;
    }
    
    public int getEc12()
    {
        return ec12;
    }
    
    public void setEc12(int ec12)
    {
        this.ec12 = ec12;
    }
    
    public int getEc13()
    {
        return ec13;
    }
    
    public void setEc13(int ec13)
    {
        this.ec13 = ec13;
    }
    
}
